package library.common.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.os.Build;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Created by qiang.hou on 2018/10/10.
 * @version 1.0
 */
public class AppUtils {

    /**
     * get app package names
     *
     * @param context Context
     * @return All the apps' package names
     */
    public static List<String> getAppPackageNames(Context context) {
        List<String> packageNames = new ArrayList<>();
        try {
            PackageManager packageManager = context.getPackageManager();
            List<PackageInfo> packageInfo = packageManager.getInstalledPackages(
                    Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ? PackageManager.MATCH_UNINSTALLED_PACKAGES : PackageManager.GET_UNINSTALLED_PACKAGES);
            for (PackageInfo pi : packageInfo) {
                packageNames.add(pi.packageName);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return packageNames;
    }

    /**
     * 获取应用签名
     *
     * @param context Context
     * @return signature
     */
    public static String getSignature(Context context) {
        try {
            /*
             * 通过包管理器获得指定包名包含签名的包信息
             */
            @SuppressLint("PackageManagerGetSignatures")
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
            /*
             * 通过返回的包信息获得签名数组
             */
            Signature[] signatures = packageInfo.signatures;
            /*
             * 获取应用签名
             */
            return signatures[0].toCharsString();
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 判断App是否已安装可用
     *
     * @param context Context
     * @return boolean
     */
    public static boolean isAppAvailable(Context context) {
        List<String> packageNames = getAppPackageNames(context);
        if (null == packageNames) {
            return false;
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(context.getPackageName());
    }

    /**
     * 判断App是否已安装可用
     *
     * @param context     Context
     * @param packageName packageName
     * @return boolean
     */
    public static boolean isAppAvailable(Context context, String packageName) {
        List<String> packageNames = getAppPackageNames(context);
        if (null == packageNames) {
            return false;
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }

    /**
     * 使用FileProvider完成 app安装器的调用，实现apk代码安装
     *
     * @param context   Context
     * @param authority authority getUriForFile()的第二个参数是authority，与manifest文件中声明的authorities保持一致
     * @param apk       apk
     */
    public static void installApk(Context context, String authority, File apk) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setDataAndType(
                FileUtils.getProviderUri(context, authority, apk),
                "application/vnd.android.package-archive");
        context.startActivity(intent);
        android.os.Process.killProcess(android.os.Process.myPid());
    }
}
